using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.UI;

namespace Dolls
{
    public class uiDollsHatchNode : MonoBehaviour
    {
        public Image mFaceIcon;
        /// <summary>
        /// 0 name
        /// fighter 1 atk 2 def 3 dodge
        /// research 1 col 2 build 3 research
        /// </summary>
        public Text[] mValueArr;
        public Image mMechIcon;
        public Image mProgressMask;
        public Image[] mLoveArr;
        public GameObject mNothingMask;
        public GameObject[] mUpArr;
        public Text mPercText;

        cDollsHatchData mRefHatchData;
        cDoll mRefDoll;

        //第一页，第二页
        public int mPageIndex = 0;

        public void Init(int index)
        {
            if (index >= cDollsHatch.Inst.mNowMaxNum)
            {
                gameObject.SetActive(false);
            }
            else
            {
                gameObject.SetActive(true);
                mRefHatchData = cDollsHatch.Inst.FindByIndex(index);
                if( mRefHatchData != null )
                {
                    mRefDoll = CDollsManager.Inst.Find(mRefHatchData.mDollResId);
                    mPageIndex = 0;
                }
                RefreshPage();
            }
        }

        private void RefreshPage()
        {
            if (mRefHatchData == null)
                Clear();
            else
            {
                mValueArr[0].text = mRefDoll.Name();

                Addressables.LoadAssetAsync<Sprite>(mRefDoll.mResData.iconPath).Completed += (o) =>
                {
                    mFaceIcon.sprite = o.Result;
                };

                RefreshProgress();

                RefreshRPage();
            }


        }

        public void Update()
        {
            RefreshProgress();
        }

        private void Clear()
        {
            mNothingMask.SetActive(true);
        }

        public void Btn_PageChange()
        {
            mPageIndex = (mPageIndex + 1) % 2;
            RefreshRPage();
        }

        private void RefreshRPage()
        {
            mNothingMask.SetActive(false);
            ClearRPage();
            if (mPageIndex == 0)
                RefreshRPage0();
            else
                RefreshRPage1();
        }

        private void RefreshRPage0()
        {
            if (mRefDoll.mResData.mainType == eDollsType.FIGHTER)
            {
                mValueArr[1].text = "ATK:" + mRefDoll.mAtkAdd + "%";
                mValueArr[2].text = "DEF:" + mRefDoll.mDefAdd + "%";
                mValueArr[3].text = "DOG:" + mRefDoll.mDodgeAdd + "%";

                if (mRefDoll.mAtkAddNew > 0)
                    mUpArr[0].gameObject.SetActive(true);
                if (mRefDoll.mDefAddNew > 0)
                    mUpArr[1].gameObject.SetActive(true);
                if (mRefDoll.mDodgeAddNew > 0)
                    mUpArr[2].gameObject.SetActive(true);

                mRefDoll.mAtkAddNew = mRefDoll.mDefAddNew = mRefDoll.mDodgeAddNew = 0;
            }
            else
            {
                mValueArr[1].text = "COL:" + mRefDoll.mCollectAdd + "%";
                mValueArr[2].text = "PRO:" + mRefDoll.mProductAdd + "%";
                mValueArr[3].text = "REA:" + mRefDoll.mResearchAdd + "%";

                if (mRefDoll.mCollectAddNew > 0)
                    mUpArr[0].gameObject.SetActive(true);
                if (mRefDoll.mProductAddNew > 0)
                    mUpArr[1].gameObject.SetActive(true);
                if (mRefDoll.mResearchAddNew > 0)
                    mUpArr[2].gameObject.SetActive(true);

                mRefDoll.mCollectAddNew = mRefDoll.mProductAddNew = mRefDoll.mResearchAddNew = 0;
            }
        }

        private void RefreshRPage1()
        {
            mMechIcon.gameObject.SetActive(true);
            int love = mRefDoll.GetLove();
            mLoveArr[0].fillAmount = love >= 20 ? 1f : 0.5f;
            mLoveArr[1].fillAmount = love >= 40 ? 1f : (love >= 30 ? 0.5f : 0);
            mLoveArr[2].fillAmount = love >= 60 ? 1f : (love >= 50 ? 0.5f : 0);
            mLoveArr[3].fillAmount = love >= 80 ? 1f : (love >= 70 ? 0.5f : 0);
            mLoveArr[4].fillAmount = love >= 100 ? 1f : (love >= 90 ? 0.5f : 0);
            mLoveArr[0].gameObject.SetActive(true);
            mLoveArr[1].gameObject.SetActive(true);
            mLoveArr[2].gameObject.SetActive(true);
            mLoveArr[3].gameObject.SetActive(true);
            mLoveArr[4].gameObject.SetActive(true);
        }

        private void ClearRPage()
        {
            mMechIcon.gameObject.SetActive(false);

            mValueArr[1].text = mValueArr[2].text = mValueArr[3].text = "";

            mLoveArr[0].gameObject.SetActive(false);
            mLoveArr[1].gameObject.SetActive(false);
            mLoveArr[2].gameObject.SetActive(false);
            mLoveArr[3].gameObject.SetActive(false);
            mLoveArr[4].gameObject.SetActive(false);

            mUpArr[0].gameObject.SetActive(false);
            mUpArr[1].gameObject.SetActive(false);
            mUpArr[2].gameObject.SetActive(false);

            mPercText.text = "";

        }

        public void Btn_Hatch()
        {
            if (cResInst.timeCapsule > 0 && mRefHatchData.mStep < cDollsHatchDefine.Const_StepMax)
            {
                cResInst.timeCapsule--;
                cResInst.Save();
                mRefHatchData.Force_Evolution();
                RefreshPage();
                gDefine.gUIMainMap.mUIDollsHatch.RefreshCapsuleNum();
            }

        }

        private void RefreshProgress()
        {
            if (mRefHatchData != null)
            {
                float perc = mRefHatchData.GetHatchProgress();
                mProgressMask.fillAmount = 1.0f - perc;
                mPercText.text = (((int)(perc * 1000)) / 10f).ToString("f1") + "%";
            }
        }



    }

}

